package com.ddn.service.hall.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.ddn.mapper.hall.BathMapper;
import com.ddn.model.hall.Bath;
import com.ddn.model.hall.BathBo;
import com.ddn.service.hall.BathService;
import com.ddn.util.ResultData;
import com.ddn.util.ThreadPoolUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/*
  ❀❀❀---扎根---❀❀❀
    ❤ @libing ❤
  
*/
@Service
public class BathServiceImpl implements BathService {
    @Autowired
    private BathMapper bathMapper;

    /**
     * 查找全部洗浴信息
     * @param map
     * @return
     */
    @Override
    public ResultData findAllBath(HashMap<String, Object> map) {
        String pageNo = (String) map.get("page");
        String pageSize = (String) map.get("limit");
        PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));

        List<Bath> bathList= bathMapper.findAllBath(map);

        PageInfo<Bath> info =new PageInfo<>(bathList);

        long l = info.getTotal();
        if (bathList != null) {
            return ResultData.ok(bathList,l);
        }
        return ResultData.fail();
    }


    /**
     * 编辑洗浴信息
     * @param map
     * @return
     */
    @Override
    public ResultData updateBath(HashMap<String, Object> map) {
        int i =bathMapper.updateBath(map);
        if (i >0) {
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    /**
     * 添加洗浴信息
     * @param map
     * @return
     */
    @Override
    public ResultData addBath(HashMap<String, Object> map) {
        int i =bathMapper.addBath(map);
        if (i >0) {
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    /**
     * 删除洗浴信息
     * @param map
     * @return
     */
    @Override
    public ResultData deleteBath(HashMap<String, Object> map) {
        int i =bathMapper.deleteBath(map);
        if (i >0) {
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    /**
     * 批量删除洗浴信息
     * @param ids
     * @return
     */
    @Override
    public ResultData batchBath(List<Integer> ids) {

        int i =bathMapper.batchBath(ids);
        if (i >0) {
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    /**
     * 在此处批量管理导入的数据
     * @return
     */
    @Override
    public ResultData importBath(MultipartFile file) throws IOException {
        //加入线程池，解决海量数据导入
        //1.获取上传的文件，校验
        if(!file.isEmpty()){
            EasyExcel.read(file.getInputStream(), Bath.class, new ReadListener<Bath>() {
                List<Bath> list=new ArrayList<>();
                @Override
                public void invoke(Bath bath, AnalysisContext analysisContext) {
                    list.add(bath);
                    //每个线程最多处理2000条
                    if(list.size()>=2000){
                        List l1=new ArrayList(list);
                        list.clear();
                        //加入到线程池
                        ThreadPoolUtil.getInstence().executor.execute(()->{
                            bathMapper.importBath(l1);
                        });
                    }
                }
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    ThreadPoolUtil.getInstence().executor.execute(()->{
                        bathMapper.importBath(list);
                    });
                }
            }).doReadAll();
            //3.返回结果
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    /**
     * 在此处批量管理导出的数据
     * @return
     */
    @Override
    public List<BathBo> exportAllBath() {
        List<BathBo> bathList= bathMapper.exportAllBath();
        return bathList;
    }
}
